# MAPLE ASSIGNMENT 10
# SOLVING NONLINEAR EQUATIONS. 
#  If an explicit solution is possible, Maple may find it for you. Maple
# automatically solves expression = 0 if the expression is not equated
# to something. It also claims that using the set notation produces
# answers which can be easily manipulated.
> solve({x^2-y^2},{y});
> solve({x^3-y^3},{y});
> solve({x^4-2*x*y+y^3},{y});
# In the first example, the two solutions are found.  In the second two
# of the solutions involve complex 
# numbers so we can ignore them (I is the square root of -1). In the
# third, Maple  is saying that there is no explicit solution but because
# this is a polynomial equation , we should try a numerical method to
# find solutions 
# . The next command does that with the fsolve command. The function f
# is the implicit function defined by the equation x^4-2xy+y^3=0.
> f:=x->fsolve({x^4-2*x*y+y^3},{y});
> f(.5);
# Note that the full solution of the equation is not a function.  A
# third degree equation has either 3 or 1 
# real solutions.  The next command produces a list of solutions of the
# equation for values of x 
# from -5 to 5 in steps of 0.5.
> seq([i/2,f(i/2)],i=-10..10);
# Note that there are only one value of f(x) for x < 0 and x>1 and 3
# values for x=0.5 and x=1.  The 
# next command plots the curve defined by y=f(x).
> with(plots):
> implicitplot(x^4-2*x*y+y^3,x=-5..5,y=-10..10);
# The plot does not look that great because Maple calculates 49
# solutions to the equation and then 
# joins them with staight lines.  The next command improves the plot by
# insisting that more points be 
# plotted.
> implicitplot(x^4-2*x*y+y^3,x=-2..2,y=-3..3,numpoints=600);
# You can see that the whole curve is not a function but can be broken
# into sections which are 
# functions as the Implicit Function Theorem says.
# Maple also allows for implicit differentiation.
> implicitdiff(x^4-2*x*y+y^3,y,x);
> dydx:=unapply(",x,y);
> dydx(1,1);
# Note that (1,1) is a point on this curve as you can see by
# substituting it into the equation defining the curve. Maple does not
# appear to be able to differentiate directly a function
# defined by the fsolve command so implicitdiff is the way to get the
# slope of this function
# Changing the example now to the production function used in class, its
# level curves or contour lines are illustrated by the following
# commands  Change the axes to normal for a better looking plot. Each
# level curve defines an implicit function.
> contourplot(x^.25+x*y+y^.25,x=0..5,y=0..5);
> implicitplot({x^.25+x*y+y^.25=2,x^.25+x*y+y^.25=4,x^.25+x*y+y^.25=6},x
> =0..5,y=0..5);
# Using implicitplot gives more control over the levels plotted.  You
# can change the levels to be plotted 
# by  editing and reexecuting the above command. You can also add more
# levels if you wish. Next 
# the tangent line to a level curve is plotted along with the level
# curve.
> f:=(x,y)->x^.25+x*y+y^.25;
> fx:=unapply(diff(f(x,y),x),x,y);
> fy:=unapply(diff(f(x,y),y),x,y);
> q:=f(2,3);
> g:=(x,y)->fx(2,3)*(x-2)+fy(2,3)*(y-3);
> implicitplot({f(x,y)=q,g(x,y)=0},x=0..5,y=0..5);
# You can try some other base points than (2,3) if you wish. To rotate
# the tangent line along the level 
# curve, you can do the following.
> y1:=fsolve(f(1.5,y)=q,y);
> g1:=(x,y)->fx(1.5,y1)*(x-1.5)+fy(1.5,y1)*(y-y1);
> implicitplot({f(x,y)=q,g(x,y)=0,g1(x,y)=0},x=0..5,y=0..5 );
# CURVATURE OF SURFACES
# The domains of the following functions are restricted to {(x,y)/x>0 &
# y>0}.
> U:=(x,y)->x^(1/4)*y^(1/2);
> plot3d(U(x,y),x=0..5,y=0..10-2*x);
# This surface is concave.  Put in some axes, change the style and
# change the first orientation angle (-15 is good for me) to improve the
# appearance of the plot.  Now go back and change (1/4,1/2) to (1,3/4)
# or to anything where the sum of the 2 powers is > 1. The resulting
# surface is neither concave or convex. It is however quasiconcave which
# may be clearer to you if you again make some changes in the appearance
# of the plot. The next commands show  with calculus that the surface
# is not concave.
> with(linalg):
> HU:=hessian(U(x,y),[x,y]);
> det(HU);
# The 0 diagonal element in the Hessian rules out strict concavity.  The
# negative det. of the Hessian rules out concavity completely.  Now go
# back to the exponents
# (1/4,1/2) in U and try the above again.  a11 in the Hessian is
# negative and the det. of the Hessian is
# positive so the function is strictly concave. The next surface looks
# like it might be strictly convex but
# it is a little difficult to tell. From the Hessian matrix you can see
# that it is. Note that the plot can be rotated using the pointer. 
> V:=(x,y)->sqrt(x+y)/(x*y);
> plot3d(V(x,y),x=1..5,y=2..12-2*x);
> HV:=hessian(V(x,y),[x,y]);
> simplify(HV[1,1]);
> det(HV);
# Maple appears not to have heard of quasiconcavity so a little work is
# required to construct the 
# bordered Hessian. This is done next for a utility function whose graph
# you plotted earlier.
> U:=(x,y)->x*y^(3/4); 
> b:=grad(U(x,y),[x,y]);
> HU:=hessian(U(x,y),[x,y]);
> HUb:=stack(b,HU);
> b1:=matrix(1,1,[0]);
> b2:=matrix(2,1,b);
> b2:=stack(b1,b2);
> BU:=concat(b2,HUb);
> det(BU);
# Since the determinant of the bordered Hessian is positive, U is
# strictly quasiconcave. This can also be 
# seen by looking at the curvature of the level curves.
> contourplot(U(x,y),x=0..5,y=0..5);
